package com.mycompany.window;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import com.vaadin.server.FileResource;
import com.vaadin.server.VaadinService;
import com.vaadin.ui.Component;
import com.vaadin.ui.Image;
import com.vaadin.ui.Notification;
import com.vaadin.ui.UI;
import com.vaadin.ui.Upload;
import com.vaadin.ui.Upload.FailedEvent;
import com.vaadin.ui.Upload.FailedListener;
import com.vaadin.ui.Upload.FinishedEvent;
import com.vaadin.ui.Upload.FinishedListener;
import com.vaadin.ui.Upload.Receiver;
import com.vaadin.ui.Upload.SucceededEvent;
import com.vaadin.ui.Upload.SucceededListener;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class MyWindow extends Window implements Receiver,SucceededListener{
	
	private Image image = new Image("My image");
	private File file;
	private FileOutputStream fos;
	private ByteArrayOutputStream byteArrayOutputStream;
	private OutputStream outputStream;
	byte[] bFile ;
	
	
	public MyWindow() {
		// TODO Auto-generated constructor stub
		super();
		setWidth("500px");
		setHeight("500px");
		setContent(makeUploadPanel());
		
		UI.getCurrent().addWindow(this);
		
	}

	private Component makeUploadPanel() {
		// TODO Auto-generated method stub
		VerticalLayout verticalLayout = new VerticalLayout();
		
		Upload upload = new Upload("This is my first upload",this);
		upload.addListener(this);
		image.setVisible(false);
		image.setWidth("150");
		image.setHeight("150");
		verticalLayout.addComponent(upload);
		verticalLayout.addComponent(image);
		return verticalLayout;
	}

	@Override
	public OutputStream receiveUpload(String filename, String mimeType) {
		// TODO Auto-generated method stub
	
		// Create upload stream
		fos = null; // Stream to write to
		try {
		// Open the file for writing.
		
			file = new File(getFullPathDest()+filename);
			
//			if(file.exists())
//				file.mkdir();
			
			System.out.println("Il file estiste :"+	 file.exists());
			fos = new FileOutputStream(file);
			} catch (final java.io.FileNotFoundException e) {
			Notification.show(
			"Could not open file<br/>", e.getMessage(),
			Notification.TYPE_ERROR_MESSAGE);
			return null;
			}
		
		
			boolean res = file.delete();
			
			
			return fos; // Return the output stream to write to
			
		
	}


	@Override
	public void uploadSucceeded(SucceededEvent event) {
		// TODO Auto-generated method stub
		image.setVisible(true);
		image.setSource(new FileResource(file));
		
	
		System.out.println("Upload Effettuato con successo");

	}
	
	private String getFullPathDest()
    {
        return getWebappPath() + "/uploads/";
    }

    private String getWebappPath()
    {
    	
    	return VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
    }

	public FileOutputStream getFos() {
		return fos;
	}

	public void setFos(FileOutputStream fos) {
		this.fos = fos;
	}

	public OutputStream getOutputStream() {
		return outputStream;
	}

	public void setOutputStream(OutputStream outputStream) {
		this.outputStream = outputStream;
	}
	
	public byte[] getImageToSave() throws IOException{

		bFile = new byte[(int) file.length()];
		FileInputStream inputStream = new FileInputStream(file);
		inputStream.read(bFile);
		 file.deleteOnExit();
		return bFile;
	}
    

}
